package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.impl.dao.ContactDao;
import com.jiantong.user.model.Contact;
import com.jiantong.user.service.ContactReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import io.terminus.common.utils.Arguments;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Code generated by terminus code gen
 * Desc: 联系人信息读服务实现类
 * Date: 2017-02-28
 */
@Slf4j
@Service
@RpcProvider
public class ContactReadServiceImpl implements ContactReadService {

    private final ContactDao contactDao;

    @Autowired
    public ContactReadServiceImpl(ContactDao contactDao) {
        this.contactDao = contactDao;
    }

    @Override
    public Response<Contact> findById(Long Id) {
        try {
            Contact exist = contactDao.findById(Id);
            if(Arguments.isNull(exist)){
                log.error("not find contact by id:{}",Id);
                return Response.fail("contact.not.exist");
            }
            return Response.ok(contactDao.findById(Id));
        } catch (Exception e) {
            log.error("find contact by id :{} failed,  cause:{}", Id, Throwables.getStackTraceAsString(e));
            return Response.fail("contact.find.fail");
        }
    }

    @Override
    public Response<List<Contact>> findByOwnerId(Long ownerId) {
        try {

            return Response.ok(contactDao.findByOwnerId(ownerId));

        }catch (Exception e){
            log.error("find by owner id:{} fail,cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("find.contact.fail");
        }
    }
}
